//
//  EDSCustomAlertView.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/5/16.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "EDSCustomAlertView.h"
#import "UIWindow+EDSCustomAlertView.h"

@interface EDSCustomAlertView ()

@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, copy) NSString *msg;
@property (nonatomic, strong) UILabel *msgLabel;

@property (nonatomic, copy) NSString *btnCancelTitle;
@property (nonatomic, strong) UIButton *btnCancel;

@property (nonatomic, copy) NSString *btnSubmitTitle;
@property (nonatomic, strong) UIButton *btnSubmit;
@property (nonatomic, strong) UIView *bgView;
 
@property (nonatomic, copy) void(^completionBlock)(void);
@property (nonatomic, copy) void(^failedBlock)(void);


- (void)initWithTitleLabel;

- (void)initWithMsgLabel;

- (void)initWithBtnCancel;

- (void)initWithBtnSubmit;

@end

@implementation EDSCustomAlertView

- (instancetype)initWithFrame:(CGRect)frame andTitle:(NSString *)title andMsg:(NSString *)msg btnCancelTitle:(NSString *)btnCancelTitle btnSubmitTitle:(NSString *)btnSubmitTitle
{
    
    self.title = title;
    self.msg = msg;
    self.btnCancelTitle = btnCancelTitle;
    self.btnSubmitTitle = btnSubmitTitle;
    return [super initWithFrame:frame];
}

- (void)initWithSubView
{
    [super initWithSubView];
    self.backgroundColor = [UIColor whiteColor];
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 10;
    
    [self initWithTitleLabel];
    
    [self initWithMsgLabel];
    
    [self initWithBtnCancel];
    
    [self initWithBtnSubmit];
}

- (void)initWithTitleLabel
{
    CGRect frame = CGRectMake(15, 37, self.width - 30, 20);
    _titleLabel = [[UILabel alloc] initWithFrame:frame];
    _titleLabel.text = self.title;
    _titleLabel.textColor = kThemeColor;//[UIColor colorWithHexString:@"#2889ff"];
    _titleLabel.font = [UIFont systemFontOfSize:21];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
}

- (void)initWithMsgLabel
{
    CGRect frame = CGRectMake(20, _titleLabel.bottom + 20, self.width - 40, 20);
    _msgLabel = [[UILabel alloc] initWithFrame:frame];
    _msgLabel.text = self.msg;
    _msgLabel.textColor = [UIColor colorWithHexString:@"#555555"];
    _msgLabel.font = [UIFont systemFontOfSize:15];
    _msgLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_msgLabel];
}

- (void)initWithBtnCancel
{
    CGRect frame = CGRectMake(self.width * 0.5 - 15 - 110, self.height - 30 - 34, 110, 34);
    _btnCancel =[UIButton buttonWithType:UIButtonTypeCustom];
    [_btnCancel setTitle:self.btnCancelTitle forState:UIControlStateNormal];
    _btnCancel.titleLabel.font = [UIFont systemFontOfSize:15];
    [_btnCancel addTarget:self action:@selector(clickedCancel:) forControlEvents:UIControlEventTouchUpInside];
    _btnCancel.layer.cornerRadius = 34 * 0.5;
    _btnCancel.layer.masksToBounds = YES;
    [self addSubview:_btnCancel];
    if (self.btnCancelTitle && self.btnSubmitTitle)
    {
        [_btnCancel setTitleColor:[UIColor colorWithHexString:@"555555"] forState:UIControlStateNormal];
        _btnCancel.backgroundColor = [UIColor whiteColor];
        _btnCancel.layer.borderColor = [UIColor colorWithHexString:@"#999999"].CGColor;
        _btnCancel.layer.borderWidth = 1;
    }
    else if (self.btnCancelTitle && !self.btnSubmitTitle)
    {
        frame = CGRectMake((self.width - 200) * 0.5, self.height - 30 - 34, 200, 34);
        _btnCancel.backgroundColor = [UIColor colorWithHexString:@"#4498ff"];
        [_btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    else
    {
        self.btnCancel.hidden = YES;
    }
    _btnCancel.frame = frame;
    
}

- (void)initWithBtnSubmit
{
    CGRect frame = CGRectMake(self.width * 0.5 + 15, self.height - 30 - 34, 110, 34);
    
    UIView *bgView = [[UIView alloc] initWithFrame:frame];
    bgView.layer.cornerRadius = bgView.height * 0.5;
    bgView.layer.shadowColor = kThemeColor.CGColor;
    bgView.layer.shadowOpacity = 0.5;
    bgView.layer.shadowOffset = CGSizeMake(-3, 3);
    [self addSubview:bgView];
    self.bgView = bgView;
    
    _btnSubmit =[UIButton buttonWithType:UIButtonTypeCustom];
    [_btnSubmit setTitle:self.btnSubmitTitle forState:UIControlStateNormal];
    _btnSubmit.titleLabel.font = [UIFont systemFontOfSize:15];
    [_btnSubmit addTarget:self action:@selector(clickedSubmit:) forControlEvents:UIControlEventTouchUpInside];
    _btnSubmit.layer.cornerRadius = 34 * 0.5;
    _btnSubmit.layer.masksToBounds = YES;
    [bgView addSubview:_btnSubmit];
    _btnSubmit.backgroundColor = kThemeColor;//[UIColor colorWithHexString:@"#3377FF"];
    [_btnSubmit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if (self.btnCancelTitle && self.btnSubmitTitle)
    {
        
    }
    else if (!self.btnCancelTitle && self.btnSubmitTitle)
    {
        frame = CGRectMake((self.width - 200) * 0.5, self.height - 30 - 34, 200, 34);
    }
    else
    {
        _btnSubmit.hidden = YES;
    }
    _btnSubmit.frame = (CGRect){.origin={0,0}, .size=frame.size};
    self.bgView.frame = frame;
}

- (void)showWithCompletion:(void (^)(void))completionBlock failed:(void (^)(void))failedBlock
{
    self.completionBlock = completionBlock;
    self.failedBlock = failedBlock;
    if (!kCSKeyWindow.custAlertView)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.custAlertMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.custAlertView = self;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
//        [maskView addGestureRecognizer:tap];
        
        [UIView animateWithDuration:0.2 animations:^{
            maskView.alpha = 0.4;
            self.alpha = 1;
        }];
    }
}

- (void)clickedCancel:(id)sender
{
    if (self.failedBlock)
    {
        self.failedBlock();
    }
}

- (void)clickedSubmit:(id)sender
{
    if (self.completionBlock)
    {
        self.completionBlock();
    }
}

- (void)hide
{
    if (kCSKeyWindow.custAlertView)
    {
        @weakify(self);
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            self.alpha = 0;
            kCSKeyWindow.custAlertMaskView.alpha = 0;
        } completion:^(BOOL finished) {
//            @strongify(self);
//            if (self.completionBlock)
//            {
//                self.completionBlock(nil);
//            }
            [kCSKeyWindow.custAlertView removeFromSuperview];
            [kCSKeyWindow.custAlertMaskView removeFromSuperview];
            kCSKeyWindow.custAlertMaskView = nil;
            kCSKeyWindow.custAlertView = nil;
        }];
    }
}


@end






















